<?php

namespace App\Http\Service\web;

use App\Models\StoreOrder;
use App\Models\StoreProduct;
use App\Models\SystemStore;


class SystemStoreService
{
    /**
     * 流水
     * 游湖sql
     * @param $store_id
     * @return mixed
     */
    public static function storeFlow()
    {
        $data= StoreOrder::with("storeSys")
            ->select(
                'order_id',
                'store_id',
                'is_channel',
                'total_price',
                'pay_price',
                'coupon_price',
                'pay_time')
            ->get();
        if(!$data){
            throw  new  \Exception('查询失败');
        }
        return  $data;
    }



    /**
     * 审核
     * @param $id
     * @param $is_show
     * @return mixed
     * @throws \Exception
     */
    public static function systemIsShow($id, $is_show)
    {
        $result = StoreProduct::systemIsShow($id, $is_show);
        if (!$result) {
            throw  new  \Exception('错误');
        }
        return $result;
    }

    /**
     * 列表及搜索
     * @param $id
     * @param $store_name
     * @return mixed
     * @throws \Exception
     */
    public static function productListService($id, $store_name)
    {
        $where = [];
        if (!empty($id)) {
            $where[] = ['id', 'like', "%$id%"];
        }
        if (!empty($store_name)) {
            $where[] = ['store_name', 'like', "%$store_name%"];
        }
        $data = StoreProduct::productListModel($where);
        if (!$data) {
            throw  new  \Exception('错误');
        }
        return $data;
    }




    /**
     * 修改详情
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public static function editService($id)
    {
        $result = SystemStore::editModel($id);
        if (!$result) {
            throw  new  \Exception('查询错误');
        }
        return $result;
    }


    /**
     * 修改
     * @param $id
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public static function storeUpdate($id, $data)
    {

        if (empty($id)) throw new \Exception('参数错误');
        if (empty($data['name'])) throw  new  \Exception('门店名称不能为空');
        if (empty($data['shopowner'])) throw  new  \Exception('店长不能为空');
        if (empty($data['introduction'])) throw  new  \Exception('简介不能为空');
        if (empty($data['phone'])) throw  new  \Exception('手机号码不能为空');
        if (empty($data['day_time'])) throw  new  \Exception('营业开始时间不能为空');
        if (empty($data['day_time2'])) throw  new  \Exception('营业结束时间不能为空');
        if (empty($data['is_business'])) throw  new  \Exception('是否营业 不能为空');
        $result = SystemStore::storeUpdate($id, $data);
        if (!$result) {
            throw  new  \Exception('修改失败');
        }
        return $result;
    }



}
